package com.krain.corelibrary.util;

import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.os.Build;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.LinearInterpolator;

/**
 * Created by ZHANGPING129 on 2015-08-14.
 */
public class AnimatorUtil {
    /**
     * 平移动画
     *
     * @param view
     * @param begin
     * @param end
     * @param durtion
     * @param listener 动画事件监听
     */
    public static void StartXTranslation(View view, float begin, float end, int durtion, AnimatorListenerAdapter listener) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", begin, end);
        animator.setDuration(durtion);
        if (listener != null) {
            animator.addListener(listener);
        }
        animator.start();

    }

    /**
     * 平移动画
     *
     * @param view
     * @param begin
     * @param end
     * @param durtion
     */
    public static void StartXTranslation(View view, float begin, float end, int durtion) {
        StartXTranslation(view, begin, end, durtion, null);
    }


    /**
     * 平移动画
     *
     * @param view
     * @param begin
     * @param end
     * @param durtion
     */
    public static void startRotationY(View view, float begin, float end, int durtion) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotationY", begin, end);
        animator.setDuration(durtion);
        animator.start();
    }


    /**
     * 收缩动画（5.0以上可用）
     *
     * @param contentView
     */
    public static void startCircleReaval(View contentView, boolean isHide) {
        int startValue = isHide ? contentView.getWidth() : 0;
        int afterValue = isHide ? 0 : contentView.getWidth();
        if (!isHide){
            contentView.setVisibility(View.GONE);
            contentView.setVisibility(View.VISIBLE);
        }else {
            contentView.setVisibility(View.VISIBLE);
            contentView.setVisibility(View.GONE);
        }
        if (Build.VERSION.SDK_INT >= 21) {
            android.animation.Animator animator = ViewAnimationUtils.createCircularReveal(contentView, contentView.getWidth() / 2,
                    contentView.getHeight() / 2, startValue, afterValue);
            animator.setInterpolator(new LinearInterpolator());
            animator.setDuration(400);
            animator.start();
        }
    }


}
